package com.android.ddfafu.activity;


import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
import com.android.ddfafu.MapApplication;
import com.android.ddfafu.R;
import java.io.File;
import java.util.HashSet;
import java.util.Set;
public class Image_activity extends AppCompatActivity{
    private SharedPreferences sp_img;
    private SharedPreferences sp_user;
    private ImageView img_back;
    private ImageView img_delete;
    private ImageView img_display;
    private MapApplication application;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_activity);
        img_back = findViewById(R.id.img_back);
        img_delete = findViewById(R.id.img_delete);
        img_display = findViewById(R.id.img_display);
        application = (MapApplication)getApplication();


        img_display.setImageBitmap(application.getmBitmap());

        img_back.setOnClickListener((v)-> finish());
        img_delete.setOnClickListener((v -> {

            sp_img = getSharedPreferences("userImages", Context.MODE_PRIVATE);
            sp_user = getSharedPreferences("userData", Context.MODE_PRIVATE);
            //删除图片
            String path = getIntent().getStringExtra("path");
            String usernameSub = sp_user.getString("USERNAME", "").replace("@", "").replace(".", "");
            Set<String> image_path_set = sp_img.getStringSet(usernameSub, null);
            SharedPreferences.Editor edit = sp_img.edit();
            //SharedPreferences.Editor editor = edit.clear();
            boolean remove = image_path_set.remove(path);

            new Thread(()->{
                File file = new File(path);
                if (file.exists()){
                    file.delete();
                }
            }).start();

            Set<String> newSet = new HashSet<>(image_path_set);
            edit.putStringSet(usernameSub,newSet);
            edit.apply();
            edit.commit();
            if (remove){

                System.out.println("删除成功");
            }
            //Intent intent = new Intent(this,MainActivity.class);
            //intent.putExtra("delete_img","ok");
            //startActivity(intent);
            try {
                Thread.sleep(300);
                finish();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }));

    }




}
